package com.example.musicdemo.model

import android.database.Cursor
import android.graphics.Bitmap
import android.provider.MediaStore
import com.example.musicdemo.util.Constant.URI_TOU
import com.example.musicdemo.util.PinYin4JUtils
import java.io.Serializable
import java.util.*
import kotlin.collections.ArrayList

data class Music(
    var id //歌曲的id
    : Long,
    var name: String?,
    var ar: List<Singer>?,
    var al: Album?,
    var dt: Int,
    var fee: Int,
    var picUriHD: Bitmap?,
    var data: String?,
    var size: Long,
    var url: String,
    var lrc:String,
    var sortLetters: String?
) : Serializable {

    fun getSongUrl(): String {
        return "$URI_TOU/song/url?id=$id"
    }


    fun getSinger(): String? {
        var singerName: String? = ""
        if (ar == null) return singerName
        ar?.apply {
            for (i in 0 until size) {
                if (i == 0) singerName = get(i).name
                if (i > 0) singerName += " ${get(i).name}"
            }
        }
        return singerName

    }

    inner class Singer : Serializable {
        var id: Int? = null
        var name: String? = null

    }

    inner class Album() : Serializable {
        var id: Long? = null
        var name: String? = null
        var picUrl: String? = null

    }

    companion object {
        fun getMusicBean(cursor: Cursor?): Music {
            //创建bean对象
            val music = Music(0, "", ArrayList<Singer>(), null, 0, 0, null, null, 0, "","","")

            cursor?.let { it ->
                it.getString(it.getColumnIndex(MediaStore.Audio.Media.DATA))
                    .also { music.data = it }
                music.size = it.getLong(it.getColumnIndex(MediaStore.Audio.Media.SIZE))

                val displayName =
                    it.getString(it.getColumnIndex(MediaStore.Audio.Media.DISPLAY_NAME))
                music.name = displayName?.substring(0, displayName.lastIndexOf("."))
                val also1 = it.getString(it.getColumnIndex(MediaStore.Audio.Media.ARTIST))
                    .also {
                        val singer = music.Singer()
                        singer.name = it
                        (music.ar as ArrayList).add(singer)
                    }
                also1
            }
            return music
        }

        /**
         * 根据特定位置cursor获取整个播放列表
         */
        fun getMusicBeans(cursor: Cursor?): ArrayList<Music> {
            val list = ArrayList<Music>()
            cursor?.let {
                it.moveToPosition(-1)
                while (it.moveToNext()) {
                    val audioBean = getMusicBean(it)
                    if (audioBean.sortLetters?.isEmpty() == true){
                        val pinyin1 = PinYin4JUtils.hanziToPinyin(audioBean.name)
                        println("我想看看这字母 $pinyin1")
                        val sortString1 = pinyin1.substring(0, 1).uppercase(Locale.getDefault())
                        if (sortString1.matches( Regex( "[A-Z]"))) {
                            audioBean.sortLetters = sortString1.uppercase(Locale.getDefault())
                        } else {
                            audioBean.sortLetters = "#"
                        }
                    }
                    list.add(audioBean)
                }
            }
            return list
        }
    }


}